//
//  TTBeautyManagerSearchResultController.m
//  TaTaBang
//
//  Created by 李禹 on 15/8/30.
//  Copyright (c) 2015年 hailong. All rights reserved.
//

#import "TTBeautyManagerSearchResultController.h"
#import "NJSettingTextFieldItem.h"
#import "NJSettingGroup.h"
#import "NJProductCell.h"
#import "NJSettingArrowItem.h"
#import "TTSettingLabelItem.h"
#import "TTBeautyManagerDetailViewController.h"

@interface TTBeautyManagerSearchResultController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TTBeautyManagerSearchResultController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        // 第一组数据
        NJSettingItem *item00 = [[TTSettingLabelItem alloc] initWithTitle:@"手机号" detailText:@"请输入手机号"];
        NJSettingItem *item01 = [[TTSettingLabelItem alloc] initWithTitle:@"犬主名" detailText:@"安在宣"];
        NJSettingItem *item02 = [[TTSettingLabelItem alloc] initWithTitle:@"会员卡余额" detailText:@"￥1500.0" textColor:[UIColor redColor]];

        NJSettingGroup *group1 = [[NJSettingGroup alloc] init];
        group1.items = @[item00, item01, item02] ;
        
        _dataArray = [NSMutableArray array];
        [_dataArray addObject:group1];
    }
    return _dataArray;
}


- (id)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (id)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    
//    [self.dataArray addObject:dataSource];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"美容服务";
//    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger count = self.dataSource.count ? self.dataSource.count : 2;
    return self.dataArray.count+count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        // 先取出对应组的小数组
        NJSettingGroup *g = self.dataArray[section];
        // 返回小数组的长度
        return g.items.count;
    }else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        // 1.创建cell
        NJProductCell *cell = [NJProductCell cellWithTableView:tableView];
        
        // 先取出对应组的组模型
        NJSettingGroup *g = self.dataArray[indexPath.section];
        //  从组模型中取出对应行的模型
        NJSettingItem *item = g.items[indexPath.row];
        cell.item = item;
        
        // 3.返回cell
        return cell;
    }else {
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.contentView.backgroundColor = [UIColor redColor];
        }
        return cell;
    }
}

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 44;
    }else {
        return 80;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 立即取消选中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        // 先取出对应组的组模型
        NJSettingGroup *g = self.dataArray[indexPath.section];
        //  从组模型中取出对应行的模型
        NJSettingItem *item = g.items[indexPath.row];
        // 判断block中是否保存了代码
        if (item.option != nil) {
            // 如果保存,就执行block中保存的代码
            item.option();
        }else if ([item isKindOfClass:[NJSettingArrowItem class]]) {
            
        }
    }else {
        TTBeautyManagerDetailViewController *detailVc = [[TTBeautyManagerDetailViewController alloc] init];
        [self.navigationController pushViewController:detailVc animated:YES];
    }
}

@end
